import type { IColumnsOption } from 'vc';
import { getImageUrl, handleTagNegativeShow, setHrefUrlTool, toLawDetail } from '@/utils';
import {
  INDEX
} from '@/utils';
import { HbSubjectEnum } from '../../apis';
import { Clamp } from 'vc';

export function useTable(hbSubjectType: HbSubjectEnum) {
  /** 环境信用表格列 */
  const appraise_columns = ref<IColumnsOption[]>([
    { ...INDEX },
    {
      title: '评价年度',
      field: 'tagYear',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'tagYear'
    },
    {
      title: '评价等级',
      field: 'tagNegative',
      width: 100,
      align: 'center',
      slots: {
        default: ({ row }) => {
          const map = {
            良好: 'table-level-1e83eb',
            诚信: 'table-level-28b97f',
            一般: 'table-level-ff9d54',
            警示: 'table-level-ff6b6a',
            不良: 'table-level-717171'
          };
          const showText = handleTagNegativeShow(row.tagNegative, row.evaluationResult) || '';
          return <span class={'table-level ' + map[showText]}>{showText}</span>;
        }
      }
    },
    {
      title: '较上次变化',
      field: 'rateChangeTrend',
      width: 120,
      align: 'center',
      slots: {
        default: ({ row }) => {
          // 0不变，-1下降，1上升，2无可比较
          const map = {
            '1': { text: '上升', img: 'up.png' },
            '-1': { text: '下降', img: 'down.png' },
            '0': { text: '不变', img: 'unchanged.png' },
            '2': { text: '无可比较', img: 'unchanged.png' }
          };
          const trend = row.rateChangeTrend ? row.rateChangeTrend + '' : '0';
          return (
            <div class="trend-wrapper">
              <span>{map[trend].text}</span>
              <img v-show={row.rateChangeTrend !== 2} class="trend-suffix"
                   src={getImageUrl(map[trend].img)}/>
            </div>
          );
        }
      }
    },
    {
      title: '披露等级',
      field: 'evaluationResult',
      width: 100,
      align: 'center'
    },
    {
      title: '披露日期',
      field: 'noticeDate',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'noticeDate'
    },
    {
      title: '评价单位',
      field: 'postAgency',
      width: 200
    },
    {
      field: 'tagName',
      title: '信息来源',
      align: 'center',
      slots: {
        default: ({ row }) => {
          return row.tagName ? (
            <Clamp
              class={[row.fileUrl ? 'text-[#1382EF] cursor-pointer hover:underline' : '']}
              maxLines={2}
              autoResize
              text={row.tagName}
              onClick={row.fileUrl ? () => setHrefUrlTool(row.fileType, row.fileUrl, '', '') : () => {
              }}
            />
          ) : (
            '-'
          );
        }
      }
    }
  ]);
  /** 黑名单表格列 */
  const blacklist_columns = ref<IColumnsOption[]>([
    { ...INDEX },
    {
      title: '评价年度',
      field: 'tagYear',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'tagYear'
    },
    {
      title: '披露日期',
      field: 'noticeDate',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'noticeDate'
    },
    {
      title: '认定单位',
      field: 'postAgency',
      width: 200,
      align: 'center'
    },
    {
      title: '列入日期',
      field: 'TBD',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'TBD'
    },
    {
      title: '移除日期',
      field: 'TBD',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'TBD'
    },
    {
      field: 'tagName',
      title: '信息来源',
      align: 'center',
      slots: {
        default: ({ row }) => {
          return row.tagName ? (
            <Clamp
              class={[row.fileUrl ? 'text-[#1382EF] cursor-pointer hover:underline' : '']}
              maxLines={2}
              autoResize
              text={row.tagName}
              onClick={row.fileUrl ? () => setHrefUrlTool(row.fileType, row.fileUrl, '', '') : () => {
              }}
            />
          ) : (
            '-'
          );
        }
      }
    }
  ]);
  /** 环保处罚表格列 */
  const penalties_columns = ref<IColumnsOption[]>([
    { ...INDEX },
    {
      title: '披露日期',
      field: 'noticeDate',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'noticeDate'
    },
    {
      title: '处罚决定日',
      field: 'penaltyDate',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'penaltyDate'
    },
    {
      title: '违法类型',
      field: 'reasonForViolation',
      width: 250,
      sortBy: 'reasonForViolation'
    },
    {
      title: '处罚类型',
      field: 'penaltyResult',
      width: 150,
      slots: {
        default: ({ row }) => {
          let showResult = '-';
          if (row.penaltyResult && row.penaltyResult.length) {
            showResult = row.penaltyResult.map((o) => o.name).join(', ');
          }
          return showResult;
        }
      }
    },
    {
      title: '处罚金额',
      field: 'penaltyAmount',
      width: 100,
      align: 'center',
      sortable: true,
      sortBy: 'penaltyAmount',
      slots: {
        default: ({ row }) => {
          let showResult = '-';
          if (row.penaltyAmount) {
            showResult = row.penaltyAmount + '万';
          }
          return showResult;
        }
      }
    },
    {
      title: '违法依据',
      field: 'relevantLawsIllegalActivity',
      width: 250,
      slots: {
        default: ({ row }) => {
          if (row.relevantLawsIllegalActivities?.length === 0) {
            return '-';
          }
          return (
            row?.relevantLawsIllegalActivities?.map((item) => {
              if (item.lawUrl) {
                return <a title={item.lawName} class="companyName line-clamp"
                  onClick={() => toLawDetail(encodeURIComponent(JSON.stringify(item)))}
                  v-html={item.lawName || '-'}></a>;
              } else {
                return <span title={item.lawName || ''} v-html={item.lawName || '-'}
                  class="line-clamp"></span>;
              }
            }) || <span title={row.relevantLawsIllegalActivity || ''}
              v-html={row.relevantLawsIllegalActivity || '-'} class="line-clamp"></span>
          );
        }
      }
    },
    {
      title: '处罚依据',
      field: 'relevantLaw',
      width: 250,
      showOverflow: 'title',
      slots: {
        default: ({ row }) => {
          if (row.relevantLaws?.length === 0) {
            return '-';
          }
          return (
            row?.relevantLaws?.map((item) => {
              if (item.lawUrl) {
                return <a title={item.lawName} class="companyName line-clamp"
                  onClick={() => toLawDetail(encodeURIComponent(JSON.stringify(item)))}
                  v-html={item.lawName || '-'}></a>;
              } else {
                return <span title={item.lawName || ''} v-html={item.lawName || '-'}
                  class="line-clamp"></span>;
              }
            }) || <span title={row.relevantLaw || ''} v-html={row.relevantLaw || '-'}
              class="line-clamp"></span>
          );
        }
      }
    },
    {
      title: '违法事实',
      field: 'violationReasonSummary',
      width: 250,
      sortBy: 'violationReasonSummary'
    },
    {
      title: '处罚内容',
      field: 'approvalSummary',
      width: 250,
      sortBy: 'approvalSummary'
    },
    {
      title: '决定文书号',
      field: 'documentNumber',
      width: 150,
      sortBy: 'documentNumber'
    },
    {
      title: '处罚机关',
      field: 'competentDepartment',
      width: 150,
      align: 'center',
      sortBy: 'competentDepartment'
    },
    {
      field: 'title',
      title: '信息来源',
      width: 300,
      align: 'center',
      slots: {
        default: ({ row }) => {
          return row.title ? (
            <Clamp
              class={[row.fileUrl ? 'text-[#1382EF] cursor-pointer hover:underline' : '']}
              maxLines={2}
              autoResize
              text={row.title}
              onClick={row.fileUrl ? () => setHrefUrlTool(row.fileType, row.fileUrl, '', '') : () => {
              }}
            />
          ) : (
            '-'
          );
        }
      }
    }
  ]);

  return {
    columns: hbSubjectType === HbSubjectEnum.ENVIRONMENTCREDITRATE ? appraise_columns : hbSubjectType === HbSubjectEnum.BLACKLIST ? blacklist_columns : penalties_columns
  };
}
